/*
 * @Autor: Ruohong Yu
 * @Date: 2023-04-01 17:48:07
 * @Description: file content
 * @FilePath: \ebook\src\api\shelf.js
 */
import { getToken } from '@/utils/localStorage'
import axios from 'axios'
// import router from '../router/index'

axios.interceptors.request.use(
  config => {
    if (getToken()) { // 判断是否存在token
      config.headers.authorization = getToken()
    }
    return config
  },
  err => {
    return Promise.reject(err)
  }
)

export function addUserShelfBook (bookName, userName, groupName, isprivate, haveRead) {
  return axios({
    method: 'post',
    url: `${process.env.VUE_APP_BOOK_URL}/my/addshelf`,
    headers: {
      authorization: getToken()
    },
    data: {
      bookname: bookName,
      username: userName,
      groupname: groupName,
      private: isprivate,
      haveread: haveRead
    }
  })
}

export function getUserShelfBook (userName) {
  return axios({
    method: 'post',
    url: `${process.env.VUE_APP_BOOK_URL}/my/getshelf`,
    headers: {
      authorization: getToken()
    },
    data: {
      username: userName
    }
  })
}

export function delUserShelfBook (userName, bookname) {
  return axios({
    method: 'post',
    url: `${process.env.VUE_APP_BOOK_URL}/my/delshelf`,
    headers: {
      authorization: getToken()
    },
    data: {
      username: userName,
      bookname: bookname
    }
  })
}

export function changeShelfPrivate (isprivate, userName, bookList) {
  return axios({
    method: 'post',
    url: `${process.env.VUE_APP_BOOK_URL}/my/setprivate`,
    data: {
      username: userName,
      booklist: bookList,
      private: isprivate
    }
  })
}

export function setHaveRead (haveread, username, bookname) {
  return axios({
    method: 'post',
    url: `${process.env.VUE_APP_BOOK_URL}/my/sethaveread`,
    data: {
      haveread,
      username,
      bookname
    }
  })
}

export function changeShelfGroup (groupName, userName, bookList) {
  return axios({
    method: 'post',
    url: `${process.env.VUE_APP_BOOK_URL}/my/removeGroup`,
    data: {
      username: userName,
      groupname: groupName,
      booklist: bookList
    }
  })
}
